﻿using MaiPlanServer.IRepository;
using MaiPlanServer.IServices.Production;
using MaiPlanServer.Model;
using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Diagnostics.SymbolStore;
using System.Text;
using System.Threading.Tasks;

namespace MaiPlanServer.Services.Production
{
    public class ProcessDesignServices : IProcessDesignServices
    {
        public ProcessDesignServices(IBaseRepository conn)
        {
            Conn = conn;
        }

        public IBaseRepository Conn { get; }

        public async Task<bool> AddProcess(pro_process pro)
        {
            bool bol= await Conn.InsertAsync(pro);

            return bol;
        }

        public async Task<bool> AddProcessContent(pro_process_content pro)
        {
            bool bol = await Conn.InsertAsync(pro);

            return bol;
        }

        public async Task<bool> DelProcess(string Id)
        {
            var sql = "delete pro_process where process_id=@Id";
            var sqls = "delete pro_process_content where process_id=@Id";
            bool bol=await Conn.DeleteAsync(sql, new { Id = Id });
           await Conn.DeleteAsync(sqls, new { Id = Id });

            return bol;
        }

        public async Task<bool> DelProcessContent(string Id)
        {
            var sql = "delete pro_process_content where content_id=@Id";
            bool bol = await Conn.DeleteAsync(sql, new { Id = Id });

            return bol;
        }

        public async Task<IEnumerable<pro_process_content>> GetPro_ProcessContent(string Id)
        {
            var sql = "select * from pro_process_content where process_id=@Id";

            var list = await Conn.QueryAsync<pro_process_content>(sql, new { Id = Id });

            return list;
        }

        public async Task<IEnumerable<pro_process>> GetPro_Processes(int PageIndex, int PageSize, string process_code, string process_name, string status)
        {
            var sql = "select * from pro_process where process_code like @process_code and process_name like @process_name and enable_flag like @status";

            sql += " order by create_time desc offset @PageSize*(@PageIndex-1) rows fetch next @PageSize row only";

            var list = await Conn.QueryAsync<pro_process>(sql,new { PageIndex, PageSize, process_code ="%"+ process_code + "%", process_name = "%" + process_name + "%", status = "%"+ status+"%" });

            return list;
        }

        public async Task<pro_process> GetPro_Processes(string Id)
        {
            var sql = "select * from pro_process where process_id=@Id";

            var data=await Conn.QueryFirstOrDefaultAsync<pro_process>(sql,new { Id = Id });

            return data;
        }

        public async Task<int> GetPro_ProcessesCount(string process_code, string process_name, string status)
        {
            var sql = "select count(1) from pro_process where process_code like @process_code and process_name like @process_name and enable_flag like @status";
            var count = await Conn.GetRecoreCountAsync(sql, new { process_code = "%" + process_code + "%", process_name = "%" + process_name + "%", status = "%" + status + "%" });

            return count;
        }

        public async Task<bool> UpdatePro_Process(pro_process pro)
        {
            bool bol  = await Conn.UpdateAsync(pro,"process_id");

            return bol;
        }
    }
}
